﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.Web.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;

namespace cvn.common
{
    public static class SecurityHandler
    {


        public const string configPath = "~/web.config";


        public static string GetMembershipSettings(string elementName)
        {
            Configuration NexConfig = WebConfigurationManager.OpenWebConfiguration(configPath);
            MembershipSection section = (MembershipSection)NexConfig.GetSection("system.web/membership");
            ProviderSettingsCollection settings = section.Providers;
            NameValueCollection membershipParams = settings[section.DefaultProvider].Parameters;
            NameValueCollection config = membershipParams;
            return config[elementName].ToString();
        }


        public static string CreateHashCode(string CodeToEncrypt)
        {
            byte[] byteEncrypted = UnicodeEncoding.UTF8.GetBytes(CodeToEncrypt);
            string strEncrypted = Convert.ToBase64String(Cryptographer.CreateHash("SHA512Managed", byteEncrypted));
            Array.Clear(byteEncrypted, 0, byteEncrypted.Length);
            return strEncrypted;

        }

        public static bool CompareHashCode(string EncryptedCode, string TextToCompare)
        {
            if (Cryptographer.CompareHash("SHA512Managed", Encoding.UTF8.GetBytes(EncryptedCode), Convert.FromBase64String(TextToCompare)))
            {
                return true;
            }
            else
            {
                return false;
            } 
        }
       
       
       
        #region Utility Methods

        /// <summary>
        /// Check the password format based upon the MembershipPasswordFormat.
        /// </summary>
        /// <param name="password">Password</param>
        /// <param name="dbpassword"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        //private bool CheckPassword(string password, string dbpassword)
        //{
        //    string pass1 = password;
        //    string pass2 = dbpassword;

        //    switch (PasswordFormat)
        //    {
        //        case MembershipPasswordFormat.Encrypted:
        //            pass2 = UnEncodePassword(dbpassword);
        //            break;
        //        case MembershipPasswordFormat.Hashed:
        //            pass1 = EncodePassword(password);
        //            break;
        //        default:
        //            break;
        //    }

        //    if (pass1 == pass2)
        //    {
        //        return true;
        //    }

        //    return false;
        //}

        ///// <summary>
        ///// UnEncode password.
        ///// </summary>
        ///// <param name="encodedPassword">Password.</param>
        ///// <returns>Unencoded password.</returns>
        //private string UnEncodePassword(string encodedPassword)
        //{
        //    string password = encodedPassword;

        //    switch (PasswordFormat)
        //    {
        //        case MembershipPasswordFormat.Clear:
        //            break;
        //        case MembershipPasswordFormat.Encrypted:
        //            password =
        //              Encoding.Unicode.GetString(DecryptPassword(Convert.FromBase64String(password)));
        //            break;
        //        case MembershipPasswordFormat.Hashed:
        //            //HMACSHA1 hash = new HMACSHA1();
        //            //hash.Key = HexToByte(machineKey.ValidationKey);
        //            //password = Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));

        //            throw new ProviderException("Not implemented password format (HMACSHA1).");
        //        default:
        //            throw new ProviderException("Unsupported password format.");
        //    }

        //    return password;
        //}

        ///// <summary>
        ///// Get config value.
        ///// </summary>
        ///// <param name="configValue"></param>
        ///// <param name="defaultValue"></param>
        ///// <returns></returns>
        //private string GetConfigValue(string configValue, string defaultValue)
        //{
        //    if (String.IsNullOrEmpty(configValue))
        //    {
        //        return defaultValue;
        //    }

        //    return configValue;
        //}

        ///// <summary>
        ///// Encode password.
        ///// </summary>
        ///// <param name="password">Password.</param>
        ///// <returns>Encoded password.</returns>
        //private string EncodePassword(string password)
        //{
        //    string encodedPassword = password;

        //    switch (PasswordFormat)
        //    {
        //        case MembershipPasswordFormat.Clear:
        //            break;
        //        case MembershipPasswordFormat.Encrypted:
        //            byte[] encryptedPass = EncryptPassword(Encoding.Unicode.GetBytes(password));
        //            encodedPassword = Convert.ToBase64String(encryptedPass);
        //            break;
        //        case MembershipPasswordFormat.Hashed:
        //            HMACSHA1 hash = new HMACSHA1();
        //            hash.Key = HexToByte(machineKey.ValidationKey);
        //            encodedPassword =
        //              Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
        //            break;
        //        default:
        //            throw new ProviderException("Unsupported password format.");
        //    }

        //    return encodedPassword;
        //}

        ///// <summary>
        ///// Converts a hexadecimal string to a byte array. Used to convert encryption key values from the configuration
        ///// </summary>
        ///// <param name="hexString"></param>
        ///// <returns></returns>
        ///// <remarks></remarks>
        //private byte[] HexToByte(string hexString)
        //{
        //    byte[] returnBytes = new byte[hexString.Length / 2];
        //    for (int i = 0; i < returnBytes.Length; i++)
        //        returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
        //    return returnBytes;
        //}

        #endregion     
    }
}
